using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;

namespace Lu62Viewer
{
    public partial class ErrorReport : Form
    {
        public ErrorReport(MyCompareResult _res)
        {
            InitializeComponent();
            m_res = _res;
            buildReport(m_res);
            rtbReport.Text = m_sb.ToString();
        }

        private StringBuilder m_sb;
        private Font printFont;
        private StringReader streamToPrint;
        private MyCompareResult m_res;

        private void buildReport(MyCompareResult _res)
        {
            // Costruiamo il report
            m_sb = new StringBuilder();
            m_sb.AppendLine(string.Format("Report generated on {0}", DateTime.Now.ToString()));
            m_sb.AppendLine("-------------------------------------");
            m_sb.AppendLine(string.Format("Comparison between two segments: {0} [{1} fields] and {2} [{3} fields]", _res.SegmentAName, _res.CountFieldsA, _res.SegmentBName, _res.CountFieldsB));
            m_sb.AppendLine("");
            bool checkNome = false;
            if (_res.SegmentAName.CompareTo(_res.SegmentBName) == 0)
                checkNome = true;            
            m_sb.AppendLine(string.Format("Do the segments have the same name? {0}", checkNome.ToString()));
            m_sb.AppendLine(string.Format("Do the segments have the same field number? {0}", _res.FieldsCountCheck.ToString()));
            if (_res.FieldErrors.Count > 0)
            {
                m_sb.AppendLine("-------------------------------------");
                m_sb.AppendLine("Comparison between different fields");
                foreach (MyFieldError _mf in _res.FieldErrors)
                {
                    m_sb.AppendLine(_mf.ToString());
                }
                m_sb.AppendLine("-------------------------------------");
            }
            else
            {
                m_sb.AppendLine("No field differences found");
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure that you want to print this report on the default printer?", "Stampa", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                streamToPrint = new StringReader(m_sb.ToString());
                printFont = new Font("Arial", 10);
                PrintDocument pdoc = new PrintDocument();
                pdoc.DocumentName = string.Format("Comparison between {0} and {1}", m_res.SegmentAName, m_res.SegmentBName);
                pdoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                pdoc.Print();
            }
        }

        // The PrintPage event is raised for each page to be printed.
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while (count < linesPerPage &&
               ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {            
            Clipboard.SetText(m_sb.ToString(), TextDataFormat.UnicodeText);
        }        
    }
}